﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Runtime.InteropServices;
using System.Windows.Media.Animation;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;

namespace WpfCap
{

    public class CapDisplay : Image, IDisposable
    {
        public CapDisplay()
        {
            Application.Current.Exit += new ExitEventHandler(Current_Exit);
        }
        void Current_Exit(object sender, ExitEventArgs e)
        {
            this.Dispose();
        }
        void Set(CapPlayer p)
        {
           p._device.OnNewBitmapReady += new EventHandler(_device_OnNewBitmapReady);
           this._device = p._device;
        }
       public CapDevice _device;
        public void _device_OnNewBitmapReady(object sender, EventArgs e)
        {
            Binding b = new Binding();
            b.Source = _device;
            b.Path = new PropertyPath(CapDisplay.FramerateProperty);
            this.SetBinding(CapDisplay.FramerateProperty, b);

            this.Source = _device.BitmapSource;
        }

        public CapPlayer CapPlayer
        {
            get { return (CapPlayer)GetValue(CapPlayerProperty); }
            set { SetValue(CapPlayerProperty, value); }
        }
        public static readonly DependencyProperty CapPlayerProperty =
            DependencyProperty.Register("CapPlayer", typeof(CapPlayer), typeof(CapDisplay), new PropertyMetadata(OnCapPlayerChanged));
        
        
        static private void OnCapPlayerChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            ((CapPlayer)e.NewValue)._device.OnNewBitmapReady += new EventHandler(((CapDisplay)obj)._device_OnNewBitmapReady);
            ((CapDisplay)obj)._device = ((CapPlayer)e.NewValue)._device;
        }

        public float Framerate
        {
            get { return (float)GetValue(FramerateProperty); }
            set { SetValue(FramerateProperty, value); }
        }
        public static readonly DependencyProperty FramerateProperty =
            DependencyProperty.Register("Framerate", typeof(float), typeof(CapDisplay), new UIPropertyMetadata(default(float)));
        #region IDisposable Members

        public void Dispose()
        {
            if (_device != null)
                _device.Dispose();
        }

        #endregion
    }

    public class CapPlayer : Image,IDisposable
    {
        public CapPlayer()
        {
            initBitmap();
            Application.Current.Exit += new ExitEventHandler(Current_Exit);

            
        }

        void Current_Exit(object sender, ExitEventArgs e)
        {
            this.Dispose();
        }
        void initBitmap()
        {
            
            if (_device == null)
            {
                _device = new CapDevice();
            }
            else
            {
                _device.Start();
            }
        }

      
        
      public CapDevice _device;









        #region IDisposable Members

        public void Dispose()
        {
            if (_device != null)
                _device.Dispose();
        }

        #endregion
    }
}
